﻿Shader "Unlit/Waterfall"
{
    Properties
    {
        _FoamTilling("xy:缩放 zw:速度", Vector) = (1, 1, 0, 0)
        _FoamPow("锐化", Float) = 3
        _LightningThickness("粗细", Float) = 1
        _BackgroundColor("底色", Color) = (1,1,1,1)
        _LightningColor("颜色", Color) = (1,1,1,1)
        _FoamLerp1("前后过度", Range(0, 1)) = 0
        _FoamLerp2("左右过度", Range(0, 1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex Vertex
            #pragma fragment Fragment

            #include "UnityCG.cginc"

            struct Attributes
            {
                float4 vertex : POSITION;
                float4 uv :TEXCOORD0;
            };

            struct Varyings
            {
                float4 vertex : SV_POSITION;
                float2 uv :TEXCOORD0;
            };

            Varyings Vertex(Attributes v)
            {
                Varyings output = (Varyings)0;
                output.vertex = UnityObjectToClipPos(v.vertex);
                output.uv.xy = v.uv;
                return output;
            }

            inline float Unity_SimpleNoise_RandomValue_float(float2 uv)
            {
                return frac(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
            }

            inline float Unity_SimpleNnoise_Interpolate_float(float a, float b, float t)
            {
                return (1.0 - t) * a + (t * b);
            }

            inline float Unity_SimpleNoise_ValueNoise_float(float2 uv)
            {
                float2 i = floor(uv);
                float2 f = frac(uv);
                f = f * f * (3.0 - 2.0 * f);

                uv = abs(frac(uv) - 0.5);
                float2 c0 = i + float2(0.0, 0.0);
                float2 c1 = i + float2(1.0, 0.0);
                float2 c2 = i + float2(0.0, 1.0);
                float2 c3 = i + float2(1.0, 1.0);
                float r0 = Unity_SimpleNoise_RandomValue_float(c0);
                float r1 = Unity_SimpleNoise_RandomValue_float(c1);
                float r2 = Unity_SimpleNoise_RandomValue_float(c2);
                float r3 = Unity_SimpleNoise_RandomValue_float(c3);

                float bottomOfGrid = Unity_SimpleNnoise_Interpolate_float(r0, r1, f.x);
                float topOfGrid = Unity_SimpleNnoise_Interpolate_float(r2, r3, f.x);
                float t = Unity_SimpleNnoise_Interpolate_float(bottomOfGrid, topOfGrid, f.y);
                return t;
            }

            float Unity_SimpleNoise_float(float2 UV)
            {
                float t = 0.0;

                float freq = pow(2.0, float(0));
                float amp = pow(0.5, float(3 - 0));
                t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

                freq = pow(2.0, float(1));
                amp = pow(0.5, float(3 - 1));
                t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

                freq = pow(2.0, float(2));
                amp = pow(0.5, float(3 - 2));
                t += Unity_SimpleNoise_ValueNoise_float(float2(UV.x / freq, UV.y / freq)) * amp;

                return t;
            }

            float Unity_Remap_float(float In, float2 InMinMax, float2 OutMinMax)
            {
                return OutMinMax.x + (In - InMinMax.x) * (OutMinMax.y - OutMinMax.x) / (InMinMax.y - InMinMax.x);
            }

            float Unity_Rectangle_float(float2 UV, float Width, float Height)
            {
                float2 d = abs(UV * 2 - 1) - float2(Width, Height);
                d = 1 - d;
                return saturate(min(d.x, d.y));
            }

            float4 _FoamTilling;
            float _FoamPow;
            float _LightningThickness;
            float4 _BackgroundColor;
            float4 _LightningColor;

            float _FoamLerp1;
            float _FoamLerp2;

            float4 Fragment(Varyings i) : SV_Target
            {
                // 水面
                float lightning = Unity_SimpleNoise_float(i.uv.xy * _FoamTilling.xy + _Time.y * _FoamTilling.zw);
                lightning *= Unity_SimpleNoise_float(i.uv.xy * _FoamTilling.xy + _Time.y * _FoamTilling.zw * 0.5);

                lightning *= smoothstep(0, _FoamLerp1, i.uv.y);
                lightning *= smoothstep(0, _FoamLerp1, 1 - i.uv.y);
                lightning *= smoothstep(0, _FoamLerp2, i.uv.x);
                lightning *= smoothstep(0, _FoamLerp2, 1 - i.uv.x);

                float lightningPow = pow(lightning, _FoamPow);
                float lightningRemap = Unity_Remap_float(lightningPow, float2(0, 1), float2(-10, 10));
                lightningRemap = Unity_Rectangle_float(float2(lightningRemap, lightningRemap), _LightningThickness, _LightningThickness);

                float4 color;
                color.a = 1;
                color.rgb = lerp(_BackgroundColor.rgb, _LightningColor.rgb, lightningRemap);

                return color;
            }
            ENDCG
        }
    }
}
